package com.hserver.beetlsql.config;

import com.zaxxer.hikari.HikariDataSource;
import org.beetl.sql.clazz.kit.ClassLoaderKit;
import org.beetl.sql.core.*;
import org.beetl.sql.core.db.DBStyle;
import org.beetl.sql.core.db.MySqlStyle;
import org.beetl.sql.core.loader.SQLLoader;
import org.beetl.sql.ext.DebugInterceptor;
import cn.hserver.core.ioc.annotation.Bean;
import cn.hserver.core.ioc.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
public class SqlConfig {


    @Bean
    public SQLManager sql() {
        HikariDataSource ds = new HikariDataSource();
        String url = "jdbc:mysql://127.0.0.1:3306/mydb?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC";
        String userName = "root";
        String password = "haosql";
        String driver = "com.mysql.jdbc.Driver";
        ds.setJdbcUrl(url);
        ds.setUsername(userName);
        ds.setPassword(password);
        ds.setDriverClassName(driver);
        //主从模式
        ConnectionSource source = ConnectionSourceHelper.getMasterSlave(ds, new DataSource[]{ds});
        //SQLManagerBuilder 唯一必须的参数就是ConnectionSource
        SQLManagerBuilder builder = new SQLManagerBuilder(source);
        builder.setNc(new UnderlinedNameConversion());
        builder.setInters(new Interceptor[]{new DebugInterceptor()});
        SQLManager sqlManager = builder.build();
        return sqlManager;

    }


    /**
     * 数据源1
     *
     * @return
     */
    @Bean("data_1")
    public SQLManager sql1() {
        HikariDataSource ds = new HikariDataSource();
        String url = "jdbc:mysql://127.0.0.1:3306/mydb?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC";
        String userName = "root";
        String password = "haosql";
        String driver = "com.mysql.jdbc.Driver";
        ds.setJdbcUrl(url);
        ds.setUsername(userName);
        ds.setPassword(password);
        ds.setDriverClassName(driver);
        ConnectionSource source = ConnectionSourceHelper.getSingle(ds);
        //SQLManagerBuilder 唯一必须的参数就是ConnectionSource
        SQLManagerBuilder builder = new SQLManagerBuilder(source);
        builder.setNc(new UnderlinedNameConversion());
        builder.setInters(new Interceptor[]{new DebugInterceptor()});
        SQLManager sqlManager = builder.build();
        return sqlManager;
    }


    //数据源2
    @Bean("data_2")
    public SQLManager sql2() {
        HikariDataSource ds = new HikariDataSource();
        String url = "jdbc:mysql://127.0.0.1:3306/mydb?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC";
        String userName = "root";
        String password = "haosql";
        String driver = "com.mysql.jdbc.Driver";
        ds.setJdbcUrl(url);
        ds.setUsername(userName);
        ds.setPassword(password);
        ds.setDriverClassName(driver);
        ConnectionSource source = ConnectionSourceHelper.getSingle(ds);
        //SQLManagerBuilder 唯一必须的参数就是ConnectionSource
        SQLManagerBuilder builder = new SQLManagerBuilder(source);
        builder.setNc(new UnderlinedNameConversion());
        builder.setInters(new Interceptor[]{new DebugInterceptor()});
        SQLManager sqlManager = builder.build();
        return sqlManager;
    }
}
